package exam;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
 * User: shawn
 * Date: 2011-5-4
 * Time: 23:02:19
 */
public class PrintThread extends Thread {

    private String path;

    public PrintThread(String path) {
        this.path = path;
    }

    public void run() {
        try {
            ComThread.InitSTA();

            ActiveXComponent objWord = new ActiveXComponent("Excel.Application");

            Dispatch documents = objWord.getProperty("Workbooks").toDispatch();
            Dispatch doc = Dispatch.call(documents, "Open", path).toDispatch();

            Dispatch.call(doc, "PrintOut", new Variant(true));

            Dispatch.call(doc, "Close", new Variant(true));

            objWord.invoke("Quit", new Variant[0]);
        }
        finally {
            ComThread.Release();
        }
    }

    public static void print(String path) {
        new PrintThread(path).start();
    }
}
